package com.joshondesign.sdljava.demo;


import com.joshondesign.sdljava.*;
import com.joshondesign.sdljava.gfx.Callback;
import com.joshondesign.sdljava.gfx.Util;

/**
 * Created by IntelliJ IDEA.
 * User: josh
 * Date: 8/31/11
 * Time: 12:35 PM
 * To change this template use File | Settings | File Templates.
 */
public class Demo1 implements SDLUtil.InitCallback {
    public static void main(String ... args) {
        SDLUtil.init(new Demo1());
    }

    public void callback() {
        try {
            Util.standard_init();
            Util.dump(SDL.SDL_GetVideoInfo());
            final SDL_Surface surface = Util.createResizableWindow(800, 600);
            //draw rects and check for events
            drawRandomRects(surface);

            Util.debug_WaitForQuit(new Callback() {
                public void call(Object o) {
                    drawRandomRects(surface);
                }
            });
        } catch (SDLException e) {
            e.printStackTrace();
        }

    }

    private static void drawRandomRects(SDL_Surface surface) {
        SDL_Rect rect = new SDL_Rect();

        for(int i=0; i<100; i++) {
            rect.setX((short) (Math.random()*800));
            rect.setY((short) (Math.random()*600));
            rect.setW(1+(int)(Math.random()*600));
            rect.setH(1+(int)(Math.random()*400));

            long color = SDL.SDL_MapRGB(surface.getFormat(),
                    (short) (Math.random() * 255),
                    (short) (Math.random() * 255),
                    (short) (Math.random() * 255)
            );
            SDL.SDL_FillRect(surface, rect, color);
        }
        SDL.SDL_UpdateRect(surface, 0, 0, 800, 600);
    }

    public static void p(String s) {
        System.err.println(s);
    }
}
